<?php

/*
 * Action
 * 
 * 4$
 * 
 * Trash a card from your hand. Gain a number of Silvers equal to its cost in coins.
 * When you would gain a card, you may reveal this from your hand. If you do, instead, gain a silver.
 * 
 */

class Trader extends Card implements iAction, iKingdom, iReaction, iReactingCard, iHinterlands {

    public function __construct() {
        parent::__construct("Trader", 4);
        $this->undoPlay = false;
        $this->undoBuy = true;

        $this->reactConditions = new ReactionConditions();
        $this->reactConditions->action = array(Gain);
        $this->reactConditions->actor = array(Yourself);
        $this->reactConditions->cardLocation = array(PlayHand);
        $this->reactConditions->limit = OncePerReaction;
    }

    public function getDescription() {
        return array(
            "Trash a card from your hand. Gain a number of Silvers equal to its cost in coins.",
            "When you would gain a card, you may reveal this from your hand. If you do, instead, gain a silver.");
    }

    public function getConditions() {
        return $this->reactConditions;
    }

    public function isReactRelevant(Game $game, $card, $name) {
        return true;
    }

    public function react(Game $game, $card, $name) {

        ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($this), "reveals " . nicePrint($this), $name);

        $silver = new Silver();
        $game->setTempCard($silver);
        $game->putCardInTrashDirectly_UseInCaution($card);
        ScreenDisplay::addScreenMessage($game, true, "gain " . nicePrint($silver) . " instead of " . nicePrint($card), "gains " . nicePrint($silver) . " instead of " . nicePrint($card), $name);
        return true;
    }

    public function play(Game $game, $isRealCard) {
        $playHand = $game->getPlayHand();
        if (sizeof($playHand) > 0) {
            $playHand = getUniqueCards($playHand);
            if (sizeof($playHand) > 1) {
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $playHand, 1, 1, null, "Choose card to trash");
                $card = $output[0];
            } else {
                $card = $playHand[0];
            }
            $card = $game->moveCardFromPileToPile($card, PlayHand, Trash);
            ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($card), " trashes " . ScreenDisplay::$LMSG);

            $coins = $card->getPrice($game)->getCoins();
            $silvers = array();
            for ($i = 0; $i < $coins; $i+=1) {
                $s = $game->moveCardFromPileToPile("Silver", BoardCards, DiscardPile);
                if (!$s) {
                    break;
                }
                $silvers[] = $s;
            }
            ScreenDisplay::addScreenMessage($game, false, " and gain  " . nicePrint($silvers), " and gains " . ScreenDisplay::$LMSG);
        } else {
            ScreenDisplay::addScreenMessage($game, true, "trash nothing  ", " trashes nothing");
        }
    }

}